package com.lipiao.smarthome.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lipiao.smarthome.LoginActivity;
import com.lipiao.smarthome.MainActivity;
import com.lipiao.smarthome.R;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UserFragment extends Fragment {

    View rootView;
    TextView user_name;
    TextView user_val;
    TextView run;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        user_name=rootView.findViewById(R.id.user_name);
        user_val=rootView.findViewById(R.id.user_val);
        run=rootView.findViewById(R.id.run);

        user_name.setText(MainActivity.getName());
        user_val.setText(MainActivity.getPass());

        run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            OkHttpClient client =new OkHttpClient();
                            Request request=new Request.Builder()
                                    .url("http://47.106.164.70:8889/logout")
                                    .build();
                            Response response=client.newCall(request).execute();
                            Log.d("登出状态",response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                Intent intent=new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        });

    }

}
